package remato.domain.common.impl;

import java.util.HashSet;
import java.util.Set;

import remato.common.domain.impl.base.DomainName;
import remato.common.domain.impl.internal.CategoryChild;
import remato.domain.common.Category;
import remato.domain.common.CategoryParentable;

/**
 * 
 * @author petterei
 * @version $Id: CategoryParentImpl.java,v 1.2 2006-02-06 09:06:03 petterei Exp $
 */
public abstract class CategoryParentImpl extends DomainName implements CategoryParentable {

	private Set<Category> categoryChildren;
	private boolean hasChildren;
	
	public CategoryParentImpl() {
		super();
		categoryChildren = new HashSet<Category>();
		hasChildren = false;
	}

	public Set<Category> getCategoryChildren() {
		return categoryChildren;
	}
	
	public void setCategoryChildren(Set<Category> categoryChildren) {
		this.categoryChildren = categoryChildren;
		hasChildren = false;
	}

	public boolean addCategoryChild(Category child){
		if (null != child.getCategoryParent() && ! child.getCategoryParent().equals(this) ) {
			child.getCategoryParent().removeCategoryChild(child);
		}
		((CategoryChild)child).setCategoryParent(this);
		return categoryChildren.add(child);
	}

	public boolean removeCategoryChild(Category child){
		if (child.getCategoryParent().equals(this)) {
			((CategoryChild)child).setCategoryParent(null);
		}
		return categoryChildren.remove(child);
	}

	public boolean hasChildren() {
		return hasChildren || ! categoryChildren.isEmpty();
	}
	public void setHasChildren(boolean hasChildrenValue) {
		hasChildren = hasChildrenValue;
	}
}